package com.ts.api.http.org.base;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.digest.MD5;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.base.entity.apply.BaseOrgApplyYinQiHuiRuo;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  银企惠诺投资有限公司
 *
 *  @author WJB
 */
public class ApiOrgPhoneMd5YinQiHuiRuo implements ApiOrgPhoneMd5ConvertHandler {

    private final String appid;
    private final String privateKey;

    public ApiOrgPhoneMd5YinQiHuiRuo(String appid, String privateKey) {
        this.appid = appid;
        this.privateKey = privateKey;
    }

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception{
        HashMap<String, Object> dataMap = new HashMap<>() ;
        List<String> list = new ArrayList<>();
        list.add(apiDataExchangeParameter.getPhoneMd5());
        dataMap.put("phone", list) ;
        String json = JsonUtils.toJson(dataMap);
        String sign = MD5.create().digestToHex(json.concat(privateKey));
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("sign", sign);
        apiDataExchangeParameter.appendExtendUrl(map);
        return json;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        JSONObject resData = JsonUtils.get(respJson).getJSONObject("data");
        Object success = resData.getJSONArray("result").getJSONObject(0).get("status");
        boolean status = Eq.object(success, 0);
        boolean code = Eq.str(jsonObject.getString("code"), "200");

        if (! (status && code)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        BaseOrgApplyYinQiHuiRuo orgApplyDaXinYang = new BaseOrgApplyYinQiHuiRuo();
        orgApplyDaXinYang.setName(apiApplyParameter.getUserName());
        orgApplyDaXinYang.setPhone(apiApplyParameter.getPhone());
        orgApplyDaXinYang.setArea(apiApplyParameter.getProvinceName()+"-"+apiApplyParameter.getCity());

        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  申请金额
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            orgApplyDaXinYang.setAmount(apiFieldConverter.getLoanAmountInt());
        }
        //  车
        orgApplyDaXinYang.setCar(apiFieldConverter.hasCarInfoToStr());
        //  房产情况
        orgApplyDaXinYang.setHouse(apiFieldConverter.hasRealEstateInfoToStr());
        //  年龄
        orgApplyDaXinYang.setAge(apiFieldConverter.getAgeInt());
        //  贷款目的
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanPurpose())) {
            String loanPurpose = ChooseEq.create()
                    .when(apiApplyParameter.getLoanPurpose(), "1", "旅游")
                    .when(apiApplyParameter.getLoanPurpose(), "2", "教育")
                    .when(apiApplyParameter.getLoanPurpose(), "3", "婚庆")
                    .when(apiApplyParameter.getLoanPurpose(), "4", "装修")
                    .when(apiApplyParameter.getLoanPurpose(), "5", "医疗")
                    .when(apiApplyParameter.getLoanPurpose(), "6", "个人消费")
                    .end();
            orgApplyDaXinYang.setPurpose(loanPurpose);
        }
        //  社保
        orgApplyDaXinYang.setSocial(apiFieldConverter.hasSocialSecurityToStr());
        //  逾期
        if (ValidParam.isNotEmpty(apiApplyParameter.getOverdue())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getOverdue(), "1", "无逾期")
                    .when(apiApplyParameter.getOverdue(), "2", "有逾期")
                    .when(apiApplyParameter.getOverdue(), "3", "一年以内无逾期")
                    .end();
            orgApplyDaXinYang.setOverdue(text);
        }
        //  工资发放形式
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发打卡")
                    .when(apiApplyParameter.getSalaryPayMethod(), "2", "现金收入")
                    .when(apiApplyParameter.getSalaryPayMethod(), "3", "个人转账")
                    .end();
            orgApplyDaXinYang.setSalary(text);
        }
        //  公积金
        orgApplyDaXinYang.setAccumulation(apiFieldConverter.hasProvidentFundToStr());
        //  保险
        orgApplyDaXinYang.setInsurance(apiFieldConverter.hasInsurancePolicyToStr());
        //  信用卡
        if (ValidParam.isNotEmpty(apiApplyParameter.getCreditLimit())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getCreditLimit(), "1", "无信用卡")
                    .when(apiApplyParameter.getCreditLimit(), "2", "3千以下")
                    .when(apiApplyParameter.getCreditLimit(), "3", "3千-1万")
                    .when(apiApplyParameter.getCreditLimit(), "4", "1-3万")
                    .when(apiApplyParameter.getCreditLimit(), "5", "3万以上")
                    .end("有信用卡");
            orgApplyDaXinYang.setCredit(text);
        }
        //  芝麻分
        orgApplyDaXinYang.setPoints(apiFieldConverter.getSesameCreditText());
        //  白条
        orgApplyDaXinYang.setJd(apiFieldConverter.getBtLimitText());
        //  花呗
        orgApplyDaXinYang.setAlipay(apiFieldConverter.getHbLimitText());
        //  身份证
        orgApplyDaXinYang.setIdcard(apiApplyParameter.getIdcard());
        //  性别
        orgApplyDaXinYang.setSex(apiFieldConverter.getSexText());
        //  工龄
        if (ValidParam.isNotEmpty(apiApplyParameter.getYearsService())) {
            String text = ChooseEq.create()
                    .when(apiApplyParameter.getYearsService(), "1", "0-6个月")
                    .when(apiApplyParameter.getYearsService(), "2", "6-12个月")
                    .when(apiApplyParameter.getYearsService(), "3", "12个月以上")
                    .end();
            orgApplyDaXinYang.setWork_year(text);
        }

        List<BaseOrgApplyYinQiHuiRuo> list = new ArrayList<>();
        list.add(orgApplyDaXinYang);
        HashMap<String, Object> dataMap = new HashMap<>() ;
        dataMap.put("customers", list);
        String json = JsonUtils.toJson(dataMap);
        String sign = MD5.create().digestToHex(json.concat(privateKey));
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("sign", sign);
        apiApplyParameter.appendExtendUrl(map);
        return json;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }

        JSONObject resData = JsonUtils.get(respJson).getJSONObject("data");
        Object success = resData.getJSONArray("result").getJSONObject(0).get("status");
        boolean status = Eq.object(success, 0);
        boolean code = Eq.str(jsonObject.getString("code"), "200");

        if (!(status && code)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
